# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import copy
import traceback
import json
from utils.backcode import ImportError
from django.http import JsonResponse
from utils.logger import Md_Debug
from app.manage.userManage.common.manage_query import UserManageDB
from app.manage.userManage.common.manage_add_edit_delete import UserEditeDB
from django.views.decorators.csrf import csrf_exempt
from utils.response_utils import response_change

user_manage_db = UserManageDB()

user_edit_db = UserEditeDB()


@csrf_exempt
def user_login(request):
    """用户登录"""
    try:
        body = json.loads(request.body)
        user_name = body.get('user_name', None)
        if not user_name:
            request.session.setdefault("user_account", user_name)
        add_game_back = user_manage_db.user_login(body)
    except Exception as e:
        Md_Debug.info(traceback.format_exc(e))
        add_game_back = None
        print(traceback.format_exc(e))
    if add_game_back:
        request.session['session_user_id'] = add_game_back.get("user_id")
        request.session['name'] = add_game_back.get("uer_name")
        request.session['company_post'] = add_game_back.get("company_post")
        request.session['department'] = add_game_back.get("department")
        request.session['role'] = add_game_back.get("role_id")

    response = JsonResponse(add_game_back)
    return response_change(response)


@csrf_exempt
def creat_user(request):
    """管理员创建、删除用户"""
    try:
        body = json.loads(request.body)
        creat_user = user_edit_db.creat_user(body)
    except Exception as e:
        Md_Debug.info(traceback.format_exc(e))
        print(traceback.format_exc(e))
    response = JsonResponse(creat_user)
    return response_change(response)


@csrf_exempt
def reset_password(request):
    """重置密码"""
    try:
        body = json.loads(request.body)
        body["user_id"] = request.session.get("session_user_id")
        re_passwd = user_edit_db.re_passwd(body)
    except Exception as e:
        Md_Debug.info(traceback.format_exc(e))
        print(traceback.format_exc(e))
    response = JsonResponse(re_passwd)
    return response_change(response)


@csrf_exempt
def user_list(request):
    """用户列表"""
    try:
        body = json.loads(request.body)
        user_list = user_manage_db.user_list(body)
    except Exception as e:
        Md_Debug.info(traceback.format_exc(e))
        print(traceback.format_exc(e))
    response = JsonResponse(user_list)
    return response_change(response)


@csrf_exempt
def user_list_permit(request):
    """权限管理"""
    try:
        body = json.loads(request.body)
        user_list = user_manage_db.user_list_permit(body)
    except Exception as e:
        Md_Debug.info(traceback.format_exc(e))
        print(traceback.format_exc(e))
    response = JsonResponse(user_list)
    return response_change(response)


@csrf_exempt
def all_user(request):
    """所有成员"""
    try:
        data = user_manage_db.all_user()
    except Exception as e:
        Md_Debug.info(traceback.format_exc(e))
        print(traceback.format_exc(e))
    response = JsonResponse(data)
    return response_change(response)


@csrf_exempt
def update_permit(request):
    """修改用户权限"""
    try:
        body = json.loads(request.body)
        add_permit = user_edit_db.add_permit(body)
    except Exception as e:
        Md_Debug.info(traceback.format_exc(e))
        print(traceback.format_exc(e))
    response = JsonResponse(add_permit)
    return response_change(response)


@csrf_exempt
def permit_list(request):
    """权限列表"""
    try:
        permit_list = user_manage_db.permit_list()
    except Exception as e:
        Md_Debug.info(traceback.format_exc(e))
        print(traceback.format_exc(e))
    response = JsonResponse(permit_list)
    return response_change(response)


@csrf_exempt
def user_permit(request):
    """用户对应权限"""
    try:
        user_permit = user_manage_db.user_permit()
    except Exception as e:
        Md_Debug.info(traceback.format_exc(e))
        print(traceback.format_exc(e))
    response = JsonResponse(user_permit)
    return response_change(response)


@csrf_exempt
def user_logout(request):
    """用户登出"""
    try:
        ret = dict()
        del request.session['session_user_id']
        del request.session['name']
        del request.session['company_post']
        del request.session['department']
        ret['response_code'] = ImportError.IMPORT_CODE_SUCCESS
        ret['response_msg'] = "success"
    except Exception as e:
        ret['response_code'] = ImportError.IMPORT_CODE_EXCEPTION
        ret['response_msg'] = traceback.format_exc(e)
        Md_Debug.info(traceback.format_exc(e))
        print(traceback.format_exc(e))
    response = JsonResponse(ret)
    return response_change(response)


@csrf_exempt
def change_passwd(request):
    """修改密码"""
    try:
        body = json.loads(request.body)
        update_passwd = user_edit_db.update_passwd(body)
    except Exception as e:
        Md_Debug.info(traceback.format_exc(e))
        print(traceback.format_exc(e))
    response = JsonResponse(update_passwd)
    return response_change(response)


@csrf_exempt
def show_checked(request):
    """展示用户权限"""
    try:
        body = json.loads(request.body)
        show_checked = user_edit_db.show_checked(body)
    except Exception as e:
        Md_Debug.info(traceback.format_exc(e))
        print(traceback.format_exc(e))
    response = JsonResponse(show_checked)
    return response_change(response)


def init_user(request):
    id = request.session.get("session_user_id",None)
    company_post = request.session.get("company_post",None)
    name = request.session.get("name",None)
    department = request.session.get("department",None)
    
    if all([id, company_post, name, department]):
        response = JsonResponse({"response_msg": "success", "response_code": 0})
    else:
        response = JsonResponse({"response_msg": "error", "response_code": 0})
    return response_change(response)
